﻿/// <reference path="../jquery-1.7.2-vsdoc.js" />

function requestFullscreen(element) {
    if (element.requestFullscreen) {
        element.requestFullscreen();
    } else if (element.mozRequestFullScreen) {
        element.mozRequestFullScreen();
    } else if (element.webkitRequestFullScreen) {
        element.webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT);
    } else {
        alert("Fullscreen not supported!");
    }
}

function closeFullscren() {
    if (document.cancelFullScreen) {
        document.cancelFullScreen();
    } else if (document.mozCancelFullScreen) {
        document.mozCancelFullScreen();
    } else if (document.webkitCancelFullScreen) {
        document.webkitCancelFullScreen();
    }

}

var $instructionsDialog = $('<div></div>').dialog({
    autoOpen: false,
    modal: true,
    title: 'Instructions',
    resizable: false,
    height: 600,
    width: 500,
    buttons: {
        Ok: function () {
            fullScreen();
            $(this).dialog("close");
        }
    }
});

var $endingDialog = $('<div></div>').dialog({
    autoOpen: false,
    modal: true,
    resizable: false,
    height: 600,
    width: 500,
    close: function () {
        $('#jsonResult').val(JSON.stringify(trials));
        $('form').submit();
    },
    buttons: {
        Ok: function () {
            $(this).dialog("close");
        }
    }
});

var currentTrial = -1;
var timeOutExpired = true;
var isBaseline = true;
var currentBaseline = -1

$(document).ready(function () {
    center();

    $(window).resize(function () {
        center();
    });

    $instructionsDialog.html('Space to continue');
    $instructionsDialog.dialog('open');

    $(window).keypress(function (key) {
        // space key to start
        if (currentTrial == -1) {
            if (key.which != 32)
                return;
            startExercise();
        }

        if (!timeOutExpired) {
            return;
        }
        //timeOutExpired = false;

        if (isBaseline && (currentBaseline++ == baselineTrials)) {
            isBaseline = false;
        }

        if (trials[currentTrial].SType != '0' && (key.which == "f".charCodeAt(0) || key.which == "F".charCodeAt(0))) {
            fKeyPress();
        }

        if (trials[currentTrial].SType != '0' && (key.which == "e".charCodeAt(0) || key.which == "E".charCodeAt(0))) {
            eKeyPress();
        }

        if (trials[currentTrial].SType == '0' && (key.which == "1".charCodeAt(0) || key.which == "2".charCodeAt(0) || key.which == "3".charCodeAt(0))) {
            numberKeyPress(String.fromCharCode(key.which));
        }
    });
});

function startExercise() {
    showNextTrial();
}

function fKeyPress() {
    if (trials[currentTrial].SType == 3 && trials[currentTrial].Letter != 1) {
        return;
    }
    trials[currentTrial].ReactionTime = new Date().getTime() - trialStartTime;
    trials[currentTrial].Correct = trials[currentTrial].Letter == 1;
    if (isBaseline && !trials[currentTrial].Correct) {
        $('#photoDisplay').hide();
        $('#feedback').html('Wrong key, be more careful next time!');
        $('#feedback').show();
        setTimeout(function () {
            $('#photoDisplay').show();
            $('#feedback').hide();
            showNextTrial();
        }, 3000);
    }

    showNextTrial();
}

function eKeyPress() {
    if (trials[currentTrial].SType == 3 && trials[currentTrial].Letter != 0) {
        return;
    }
    trials[currentTrial].ReactionTime = new Date().getTime() - trialStartTime;
    trials[currentTrial].Correct = trials[currentTrial].Letter == 0;
    if (isBaseline && !trials[currentTrial].Correct) {
        $('#photoDisplay').hide();
        $('#feedback').html('Wrong key, be more careful next time!');
        $('#feedback').show();
        setTimeout(function () {
            $('#photoDisplay').show();
            $('#feedback').hide();
            showNextTrial();
        }, 2500);
    }

    showNextTrial();
}

function numberKeyPress(number) {
    //    trials[currentTrial].ReactionTime = new Date().getTime() - trialStartTime;
    trials[currentTrial].Correct = trials[currentTrial].Digit == number;
    if (isBaseline && !trials[currentTrial].Correct) {
        alert('wrong answer!');
    }

    showNextTrial();
}

var trialStartTime = 0;
var picSelector = $('#picSeparator');

function showNextTrial() {
    if (currentTrial != -1) {
        $('#upPhoto_' + currentTrial).hide()
        $('#downPhoto_' + currentTrial).hide();
    }

    if (currentTrial == trials.length - 1) {
        exitExercise();
    }


    currentTrial++;


    if (trials[currentTrial].SType == '0') {
        //fixation trial
        hideBackground();
        showBackground();

        $(picSelector).html(trials[currentTrial].Digit);

        setTimeout(function () {
            hideBackground();
            showBackground();
            $(picSelector).html('');
            timeOutExpired = true;
        }, digitTime);

        console.log("crt " + currentTrial + " type: " + trials[currentTrial].SType);
    } else {
        $(picSelector).html('+');

        hideBackground();

        console.log("crt " + currentTrial + " type: " + trials[currentTrial].SType);


        $('#upPhoto_' + currentTrial).show();
        $('#downPhoto_' + currentTrial).show();

        setTimeout(function () {
            $(picSelector).css('font-size', '20px');
        }, 20);
        setTimeout(function () {
            $(picSelector).css('font-size', '23px');
        }, 60);
        setTimeout(function () {
            $(picSelector).css('font-size', '26px');
        }, 100);


        setTimeout(function () {
            center();
            hideCurrentPhotos();
            showBackground();
            displayLetter();
            timeOutExpired = true;
        }, pictureTime);
        center();
    }
}

function exitExercise() {
    hideBackground();
    closeFullscren();
    $endingDialog.html('Thank you for your time!');
    $endingDialog.dialog('open');
}

var downBackground = $('#downBackground');
var upBackground = $('#upBackground');
var upE = $('#upE');
var upF = $('#upF');
var downF = $('#downF');
var downE = $('#downE');

function displayLetter() {

    var letter = trials[currentTrial].Letter;
    var position = trials[currentTrial].LetterPosition;

    if (letter == 0 && position == 0) {
        $(upBackground).hide();
        $(upE).show();
    } else if (letter == 1 && position == 0) {
        $(upBackground).hide();
        $(upF).show();
    } else if (letter == 0 && position == 1) {
        $(downBackground).hide();
        $(downE).show();
    } else if (letter == 1 && position == 1) {
        $(downBackground).hide();
        $(downF).show();
    }

    trialStartTime = new Date().getTime();
}

function hideBackground() {
    $(upBackground).hide();
    $(downBackground).hide();
    $(upE).hide();
    $(upF).hide();
    $(downE).hide();
    $(downF).hide();
}

function showBackground() {
    $(upBackground).show();
    $(downBackground).show();
}

function hideAllPictures() {
    $('[id^="upPhoto"]').hide();
    $('[id^="downPhoto"]').hide();
}

function hideCurrentPhotos() {
    $('#upPhoto_' + currentTrial).hide()
    $('#downPhoto_' + currentTrial).hide();
}

function center() {
    $('.photoDisplay').css({
        position: 'absolute',
        left: ($(window).width() - $('.photoDisplay').outerWidth()) / 2,
        top: ($(window).height() - $('.photoDisplay').outerHeight()) / 2
    });
}


function fullScreen() {
    requestFullscreen(document.getElementById("container"));
}
